from fastapi import FastAPI, Depends
from datetime import datetime



from fastapi import Request
from fastapi.exception_handlers import request_validation_exception_handler

from fastapi.exceptions import RequestValidationError
from requests import Session

from database import get_db
from routers import patient_router, doctor_router,auth_router,pharmacist_router,worker_router,admin_router
from routers import account_router
from routers import photo_router
from routers import medicine_router
from routers import doc_schedule_router,booking_router,consultation_router,prescription_router,medicine_record_router,department_router,payment_router
from services.datetime_service import now_time
from services.system_status_service import get_system_status_service

app = FastAPI()

# http://localhost:8080/docs
# http://localhost:8080/redoc

# uvicorn main:app --reload --host 0.0.0.0 --port 8080
# uvicorn main:app --reload --host 0.0.0.0 --port 8000 在hltt的win上

@app.get("/")
def read_root():
    return {"message": "Hospital backend is running"}


@app.get("/ping/")
def ping():
    return {"message":"pong","timestamp":datetime.now().isoformat(),"status":"ok"}

@app.get("/timestamp/")
def get_time():
    return {"timestamp":now_time().isoformat()}

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    print(f"Validation error: {exc.errors()}")
    return await request_validation_exception_handler(request, exc)

@app.get("/system/status/")
def get_system_status(db: Session=Depends(get_db)):
    return get_system_status_service(db)


# 密码，登陆验证
app.include_router(auth_router.router,tags=["auth"])

# 账号
app.include_router(account_router.router,tags=["account"])

# 五类用户
app.include_router(patient_router.router,tags=["patients"])
app.include_router(doctor_router.router,tags=["doctors"])
app.include_router(pharmacist_router.router,tags=["pharmacists"])
app.include_router(worker_router.router,tags=["workers"])
app.include_router(admin_router.router,tags=["admins"])

# 药物表管理
app.include_router(medicine_router.router,tags=["medicines"])

##病人按需求查看目标医生排班信息+医生排班表的增删查改
app.include_router(doc_schedule_router.router, tags=["doc_schedules"])  #二者公用d_s路由，在对应路由中已经加了对应的tag

#预约表的增删查改+患者进行预约+医生查看自身预约和患者信息
app.include_router(booking_router.router, tags=["bookings"])

#consultation(诊断)表的增删查改
app.include_router(consultation_router.router,tags=["consultations"])

#处方表的增删查改
app.include_router(prescription_router.router,tags=["prescriptions"])

#发药表的增删查改
app.include_router(medicine_record_router.router,tags=["medicine-records"])
#科室表的增删查改
app.include_router(department_router.router,tags=["departments"])
#支付表的增删查改
app.include_router(payment_router.router,tags=["payments"])

#照片
app.include_router(photo_router.router,tags=["photo"])

